
#DENMARK

#FLAG

#INTELLIGENT

# 1. intelligent1_DK_diff ~ treat_flag
model_intelligent1_DK_diff_flag <- lm(intelligent1_DK_diff ~ treat_flag, data = df)
summary(model_intelligent1_DK_diff_flag)
margins_intelligent1_DK_diff_flag <- margins(model_intelligent1_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent1_DK_diff_flag)

# 2. intelligent2_DK_diff ~ treat_flag
model_intelligent2_DK_diff_flag <- lm(intelligent2_DK_diff ~ treat_flag, data = df)
summary(model_intelligent2_DK_diff_flag)
margins_intelligent2_DK_diff_flag <- margins(model_intelligent2_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent2_DK_diff_flag)

# 3. intelligent3_DK_diff ~ treat_flag
model_intelligent3_DK_diff_flag <- lm(intelligent3_DK_diff ~ treat_flag, data = df)
summary(model_intelligent3_DK_diff_flag)
margins_intelligent3_DK_diff_flag <- margins(model_intelligent3_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent3_DK_diff_flag)

# 4. intelligent4_DK_diff ~ treat_flag
model_intelligent4_DK_diff_flag <- lm(intelligent4_DK_diff ~ treat_flag, data = df)
summary(model_intelligent4_DK_diff_flag)
margins_intelligent4_DK_diff_flag <- margins(model_intelligent4_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent4_DK_diff_flag)

# 5. intelligent5_DK_diff ~ treat_flag
model_intelligent5_DK_diff_flag <- lm(intelligent5_DK_diff ~ treat_flag, data = df)
summary(model_intelligent5_DK_diff_flag)
margins_intelligent5_DK_diff_flag <- margins(model_intelligent5_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent5_DK_diff_flag)

# 6. intelligent6_DK_diff ~ treat_flag
model_intelligent6_DK_diff_flag <- lm(intelligent6_DK_diff ~ treat_flag, data = df)
summary(model_intelligent6_DK_diff_flag)
margins_intelligent6_DK_diff_flag <- margins(model_intelligent6_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent6_DK_diff_flag)

# 7. intelligent7_DK_diff ~ treat_flag
model_intelligent7_DK_diff_flag <- lm(intelligent7_DK_diff ~ treat_flag, data = df)
summary(model_intelligent7_DK_diff_flag)
margins_intelligent7_DK_diff_flag <- margins(model_intelligent7_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent7_DK_diff_flag)

# 8. intelligent8_DK_diff ~ treat_flag
model_intelligent8_DK_diff_flag <- lm(intelligent8_DK_diff ~ treat_flag, data = df)
summary(model_intelligent8_DK_diff_flag)
margins_intelligent8_DK_diff_flag <- margins(model_intelligent8_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent8_DK_diff_flag)

# 9. intelligent9_DK_diff ~ treat_flag
model_intelligent9_DK_diff_flag <- lm(intelligent9_DK_diff ~ treat_flag, data = df)
summary(model_intelligent9_DK_diff_flag)
margins_intelligent9_DK_diff_flag <- margins(model_intelligent9_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent9_DK_diff_flag)

# 10. intelligent10_DK_diff ~ treat_flag
model_intelligent10_DK_diff_flag <- lm(intelligent10_DK_diff ~ treat_flag, data = df)
summary(model_intelligent10_DK_diff_flag)
margins_intelligent10_DK_diff_flag <- margins(model_intelligent10_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent10_DK_diff_flag)

# 11. intelligent11_DK_diff ~ treat_flag
model_intelligent11_DK_diff_flag <- lm(intelligent11_DK_diff ~ treat_flag, data = df)
summary(model_intelligent11_DK_diff_flag)
margins_intelligent11_DK_diff_flag <- margins(model_intelligent11_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent11_DK_diff_flag)

# 12. intelligent12_DK_diff ~ treat_flag
model_intelligent12_DK_diff_flag <- lm(intelligent12_DK_diff ~ treat_flag, data = df)
summary(model_intelligent12_DK_diff_flag)
margins_intelligent12_DK_diff_flag <- margins(model_intelligent12_DK_diff_flag, variables = "treat_flag")
summary(margins_intelligent12_DK_diff_flag)


# --- Build frames for the diff-models ---
coef_list_flag_diff <- list(
  broom::tidy(model_intelligent1_DK_diff_flag)  %>% mutate(outcome = "A"),
  broom::tidy(model_intelligent2_DK_diff_flag)  %>% mutate(outcome = "V"),
  broom::tidy(model_intelligent3_DK_diff_flag)  %>% mutate(outcome = "I"),
  broom::tidy(model_intelligent4_DK_diff_flag)  %>% mutate(outcome = "F"),
  broom::tidy(model_intelligent5_DK_diff_flag)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_intelligent6_DK_diff_flag)  %>% mutate(outcome = "M"),
  broom::tidy(model_intelligent7_DK_diff_flag)  %>% mutate(outcome = "C"),
  broom::tidy(model_intelligent8_DK_diff_flag)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_intelligent9_DK_diff_flag)  %>% mutate(outcome = "B"),
  broom::tidy(model_intelligent10_DK_diff_flag) %>% mutate(outcome = "O"),
  broom::tidy(model_intelligent11_DK_diff_flag) %>% mutate(outcome = "Å"),
  broom::tidy(model_intelligent12_DK_diff_flag) %>% mutate(outcome = "D")
)


coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Define order ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Draw horizontal coefplot ---
flag_t1_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Intelligent by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag intelligent by party DK.jpeg",
  plot     = flag_t1_dk
)

#############################################
#RELIABLE
# 1. reliable1_DK_diff ~ treat_flag
model_reliable1_DK_diff_flag <- lm(reliable1_DK_diff ~ treat_flag, data = df)
summary(model_reliable1_DK_diff_flag)
margins_reliable1_DK_diff_flag <- margins(model_reliable1_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable1_DK_diff_flag)

# 2. reliable2_DK_diff ~ treat_flag
model_reliable2_DK_diff_flag <- lm(reliable2_DK_diff ~ treat_flag, data = df)
summary(model_reliable2_DK_diff_flag)
margins_reliable2_DK_diff_flag <- margins(model_reliable2_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable2_DK_diff_flag)

# 3. reliable3_DK_diff ~ treat_flag
model_reliable3_DK_diff_flag <- lm(reliable3_DK_diff ~ treat_flag, data = df)
summary(model_reliable3_DK_diff_flag)
margins_reliable3_DK_diff_flag <- margins(model_reliable3_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable3_DK_diff_flag)

# 4. reliable4_DK_diff ~ treat_flag
model_reliable4_DK_diff_flag <- lm(reliable4_DK_diff ~ treat_flag, data = df)
summary(model_reliable4_DK_diff_flag)
margins_reliable4_DK_diff_flag <- margins(model_reliable4_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable4_DK_diff_flag)

# 5. reliable5_DK_diff ~ treat_flag
model_reliable5_DK_diff_flag <- lm(reliable5_DK_diff ~ treat_flag, data = df)
summary(model_reliable5_DK_diff_flag)
margins_reliable5_DK_diff_flag <- margins(model_reliable5_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable5_DK_diff_flag)

# 6. reliable6_DK_diff ~ treat_flag
model_reliable6_DK_diff_flag <- lm(reliable6_DK_diff ~ treat_flag, data = df)
summary(model_reliable6_DK_diff_flag)
margins_reliable6_DK_diff_flag <- margins(model_reliable6_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable6_DK_diff_flag)

# 7. reliable7_DK_diff ~ treat_flag
model_reliable7_DK_diff_flag <- lm(reliable7_DK_diff ~ treat_flag, data = df)
summary(model_reliable7_DK_diff_flag)
margins_reliable7_DK_diff_flag <- margins(model_reliable7_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable7_DK_diff_flag)

# 8. reliable8_DK_diff ~ treat_flag
model_reliable8_DK_diff_flag <- lm(reliable8_DK_diff ~ treat_flag, data = df)
summary(model_reliable8_DK_diff_flag)
margins_reliable8_DK_diff_flag <- margins(model_reliable8_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable8_DK_diff_flag)

# 9. reliable9_DK_diff ~ treat_flag
model_reliable9_DK_diff_flag <- lm(reliable9_DK_diff ~ treat_flag, data = df)
summary(model_reliable9_DK_diff_flag)
margins_reliable9_DK_diff_flag <- margins(model_reliable9_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable9_DK_diff_flag)

# 10. reliable10_DK_diff ~ treat_flag
model_reliable10_DK_diff_flag <- lm(reliable10_DK_diff ~ treat_flag, data = df)
summary(model_reliable10_DK_diff_flag)
margins_reliable10_DK_diff_flag <- margins(model_reliable10_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable10_DK_diff_flag)

# 11. reliable11_DK_diff ~ treat_flag
model_reliable11_DK_diff_flag <- lm(reliable11_DK_diff ~ treat_flag, data = df)
summary(model_reliable11_DK_diff_flag)
margins_reliable11_DK_diff_flag <- margins(model_reliable11_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable11_DK_diff_flag)

# 12. reliable12_DK_diff ~ treat_flag
model_reliable12_DK_diff_flag <- lm(reliable12_DK_diff ~ treat_flag, data = df)
summary(model_reliable12_DK_diff_flag)
margins_reliable12_DK_diff_flag <- margins(model_reliable12_DK_diff_flag, variables = "treat_flag")
summary(margins_reliable12_DK_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
coef_list_flag_diff <- list(
  broom::tidy(model_reliable1_DK_diff_flag)  %>% mutate(outcome = "A"),
  broom::tidy(model_reliable2_DK_diff_flag)  %>% mutate(outcome = "V"),
  broom::tidy(model_reliable3_DK_diff_flag)  %>% mutate(outcome = "I"),
  broom::tidy(model_reliable4_DK_diff_flag)  %>% mutate(outcome = "F"),
  broom::tidy(model_reliable5_DK_diff_flag)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_reliable6_DK_diff_flag)  %>% mutate(outcome = "M"),
  broom::tidy(model_reliable7_DK_diff_flag)  %>% mutate(outcome = "C"),
  broom::tidy(model_reliable8_DK_diff_flag)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_reliable9_DK_diff_flag)  %>% mutate(outcome = "B"),
  broom::tidy(model_reliable10_DK_diff_flag) %>% mutate(outcome = "O"),
  broom::tidy(model_reliable11_DK_diff_flag) %>% mutate(outcome = "Å"),
  broom::tidy(model_reliable12_DK_diff_flag) %>% mutate(outcome = "D")
)


coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
flag_t2_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Reliable by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag reliable by party DK.jpeg",
  plot     = flag_t2_dk
)
###########################################
#SELFISH
# 1. selfish1_DK_diff ~ treat_flag
model_selfish1_DK_diff_flag <- lm(selfish1_DK_diff ~ treat_flag, data = df)
summary(model_selfish1_DK_diff_flag)
margins_selfish1_DK_diff_flag <- margins(model_selfish1_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish1_DK_diff_flag)

# 2. selfish2_DK_diff ~ treat_flag
model_selfish2_DK_diff_flag <- lm(selfish2_DK_diff ~ treat_flag, data = df)
summary(model_selfish2_DK_diff_flag)
margins_selfish2_DK_diff_flag <- margins(model_selfish2_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish2_DK_diff_flag)

# 3. selfish3_DK_diff ~ treat_flag
model_selfish3_DK_diff_flag <- lm(selfish3_DK_diff ~ treat_flag, data = df)
summary(model_selfish3_DK_diff_flag)
margins_selfish3_DK_diff_flag <- margins(model_selfish3_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish3_DK_diff_flag)

# 4. selfish4_DK_diff ~ treat_flag
model_selfish4_DK_diff_flag <- lm(selfish4_DK_diff ~ treat_flag, data = df)
summary(model_selfish4_DK_diff_flag)
margins_selfish4_DK_diff_flag <- margins(model_selfish4_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish4_DK_diff_flag)

# 5. selfish5_DK_diff ~ treat_flag
model_selfish5_DK_diff_flag <- lm(selfish5_DK_diff ~ treat_flag, data = df)
summary(model_selfish5_DK_diff_flag)
margins_selfish5_DK_diff_flag <- margins(model_selfish5_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish5_DK_diff_flag)

# 6. selfish6_DK_diff ~ treat_flag
model_selfish6_DK_diff_flag <- lm(selfish6_DK_diff ~ treat_flag, data = df)
summary(model_selfish6_DK_diff_flag)
margins_selfish6_DK_diff_flag <- margins(model_selfish6_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish6_DK_diff_flag)

# 7. selfish7_DK_diff ~ treat_flag
model_selfish7_DK_diff_flag <- lm(selfish7_DK_diff ~ treat_flag, data = df)
summary(model_selfish7_DK_diff_flag)
margins_selfish7_DK_diff_flag <- margins(model_selfish7_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish7_DK_diff_flag)

# 8. selfish8_DK_diff ~ treat_flag
model_selfish8_DK_diff_flag <- lm(selfish8_DK_diff ~ treat_flag, data = df)
summary(model_selfish8_DK_diff_flag)
margins_selfish8_DK_diff_flag <- margins(model_selfish8_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish8_DK_diff_flag)

# 9. selfish9_DK_diff ~ treat_flag
model_selfish9_DK_diff_flag <- lm(selfish9_DK_diff ~ treat_flag, data = df)
summary(model_selfish9_DK_diff_flag)
margins_selfish9_DK_diff_flag <- margins(model_selfish9_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish9_DK_diff_flag)

# 10. selfish10_DK_diff ~ treat_flag
model_selfish10_DK_diff_flag <- lm(selfish10_DK_diff ~ treat_flag, data = df)
summary(model_selfish10_DK_diff_flag)
margins_selfish10_DK_diff_flag <- margins(model_selfish10_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish10_DK_diff_flag)

# 11. selfish11_DK_diff ~ treat_flag
model_selfish11_DK_diff_flag <- lm(selfish11_DK_diff ~ treat_flag, data = df)
summary(model_selfish11_DK_diff_flag)
margins_selfish11_DK_diff_flag <- margins(model_selfish11_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish11_DK_diff_flag)

# 12. selfish12_DK_diff ~ treat_flag
model_selfish12_DK_diff_flag <- lm(selfish12_DK_diff ~ treat_flag, data = df)
summary(model_selfish12_DK_diff_flag)
margins_selfish12_DK_diff_flag <- margins(model_selfish12_DK_diff_flag, variables = "treat_flag")
summary(margins_selfish12_DK_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
coef_list_flag_diff <- list(
  broom::tidy(model_selfish1_DK_diff_flag)  %>% mutate(outcome = "A"),
  broom::tidy(model_selfish2_DK_diff_flag)  %>% mutate(outcome = "V"),
  broom::tidy(model_selfish3_DK_diff_flag)  %>% mutate(outcome = "I"),
  broom::tidy(model_selfish4_DK_diff_flag)  %>% mutate(outcome = "F"),
  broom::tidy(model_selfish5_DK_diff_flag)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_selfish6_DK_diff_flag)  %>% mutate(outcome = "M"),
  broom::tidy(model_selfish7_DK_diff_flag)  %>% mutate(outcome = "C"),
  broom::tidy(model_selfish8_DK_diff_flag)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_selfish9_DK_diff_flag)  %>% mutate(outcome = "B"),
  broom::tidy(model_selfish10_DK_diff_flag) %>% mutate(outcome = "O"),
  broom::tidy(model_selfish11_DK_diff_flag) %>% mutate(outcome = "Å"),
  broom::tidy(model_selfish12_DK_diff_flag) %>% mutate(outcome = "D")
)


coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
flag_t3_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Selfish by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag selfish by party DK.jpeg",
  plot     = flag_t3_dk
)
###################################################
#MEAN
# 1. mean1_DK_diff ~ treat_flag
model_mean1_DK_diff_flag <- lm(mean1_DK_diff ~ treat_flag, data = df)
summary(model_mean1_DK_diff_flag)
margins_mean1_DK_diff_flag <- margins(model_mean1_DK_diff_flag, variables = "treat_flag")
summary(margins_mean1_DK_diff_flag)

# 2. mean2_DK_diff ~ treat_flag
model_mean2_DK_diff_flag <- lm(mean2_DK_diff ~ treat_flag, data = df)
summary(model_mean2_DK_diff_flag)
margins_mean2_DK_diff_flag <- margins(model_mean2_DK_diff_flag, variables = "treat_flag")
summary(margins_mean2_DK_diff_flag)

# 3. mean3_DK_diff ~ treat_flag
model_mean3_DK_diff_flag <- lm(mean3_DK_diff ~ treat_flag, data = df)
summary(model_mean3_DK_diff_flag)
margins_mean3_DK_diff_flag <- margins(model_mean3_DK_diff_flag, variables = "treat_flag")
summary(margins_mean3_DK_diff_flag)

# 4. mean4_DK_diff ~ treat_flag
model_mean4_DK_diff_flag <- lm(mean4_DK_diff ~ treat_flag, data = df)
summary(model_mean4_DK_diff_flag)
margins_mean4_DK_diff_flag <- margins(model_mean4_DK_diff_flag, variables = "treat_flag")
summary(margins_mean4_DK_diff_flag)

# 5. mean5_DK_diff ~ treat_flag
model_mean5_DK_diff_flag <- lm(mean5_DK_diff ~ treat_flag, data = df)
summary(model_mean5_DK_diff_flag)
margins_mean5_DK_diff_flag <- margins(model_mean5_DK_diff_flag, variables = "treat_flag")
summary(margins_mean5_DK_diff_flag)

# 6. mean6_DK_diff ~ treat_flag
model_mean6_DK_diff_flag <- lm(mean6_DK_diff ~ treat_flag, data = df)
summary(model_mean6_DK_diff_flag)
margins_mean6_DK_diff_flag <- margins(model_mean6_DK_diff_flag, variables = "treat_flag")
summary(margins_mean6_DK_diff_flag)

# 7. mean7_DK_diff ~ treat_flag
model_mean7_DK_diff_flag <- lm(mean7_DK_diff ~ treat_flag, data = df)
summary(model_mean7_DK_diff_flag)
margins_mean7_DK_diff_flag <- margins(model_mean7_DK_diff_flag, variables = "treat_flag")
summary(margins_mean7_DK_diff_flag)

# 8. mean8_DK_diff ~ treat_flag
model_mean8_DK_diff_flag <- lm(mean8_DK_diff ~ treat_flag, data = df)
summary(model_mean8_DK_diff_flag)
margins_mean8_DK_diff_flag <- margins(model_mean8_DK_diff_flag, variables = "treat_flag")
summary(margins_mean8_DK_diff_flag)

# 9. mean9_DK_diff ~ treat_flag
model_mean9_DK_diff_flag <- lm(mean9_DK_diff ~ treat_flag, data = df)
summary(model_mean9_DK_diff_flag)
margins_mean9_DK_diff_flag <- margins(model_mean9_DK_diff_flag, variables = "treat_flag")
summary(margins_mean9_DK_diff_flag)

# 10. mean10_DK_diff ~ treat_flag
model_mean10_DK_diff_flag <- lm(mean10_DK_diff ~ treat_flag, data = df)
summary(model_mean10_DK_diff_flag)
margins_mean10_DK_diff_flag <- margins(model_mean10_DK_diff_flag, variables = "treat_flag")
summary(margins_mean10_DK_diff_flag)

# 11. mean11_DK_diff ~ treat_flag
model_mean11_DK_diff_flag <- lm(mean11_DK_diff ~ treat_flag, data = df)
summary(model_mean11_DK_diff_flag)
margins_mean11_DK_diff_flag <- margins(model_mean11_DK_diff_flag, variables = "treat_flag")
summary(margins_mean11_DK_diff_flag)

# 12. mean12_DK_diff ~ treat_flag
model_mean12_DK_diff_flag <- lm(mean12_DK_diff ~ treat_flag, data = df)
summary(model_mean12_DK_diff_flag)
margins_mean12_DK_diff_flag <- margins(model_mean12_DK_diff_flag, variables = "treat_flag")
summary(margins_mean12_DK_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
coef_list_flag_diff <- list(
  broom::tidy(model_mean1_DK_diff_flag)  %>% mutate(outcome = "A"),
  broom::tidy(model_mean2_DK_diff_flag)  %>% mutate(outcome = "V"),
  broom::tidy(model_mean3_DK_diff_flag)  %>% mutate(outcome = "I"),
  broom::tidy(model_mean4_DK_diff_flag)  %>% mutate(outcome = "F"),
  broom::tidy(model_mean5_DK_diff_flag)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_mean6_DK_diff_flag)  %>% mutate(outcome = "M"),
  broom::tidy(model_mean7_DK_diff_flag)  %>% mutate(outcome = "C"),
  broom::tidy(model_mean8_DK_diff_flag)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_mean9_DK_diff_flag)  %>% mutate(outcome = "B"),
  broom::tidy(model_mean10_DK_diff_flag) %>% mutate(outcome = "O"),
  broom::tidy(model_mean11_DK_diff_flag) %>% mutate(outcome = "Å"),
  broom::tidy(model_mean12_DK_diff_flag) %>% mutate(outcome = "D")
)


coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
flag_t4_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Mean by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag mean by party DK.jpeg",
  plot     = flag_t4_dk
)
#########################################################
#CAKE
########################################################

#DENMARK

#cake

#INTELLIGENT

# 1. intelligent1_DK_diff ~ treat_cake
model_intelligent1_DK_diff_cake <- lm(intelligent1_DK_diff ~ treat_cake, data = df)
summary(model_intelligent1_DK_diff_cake)
margins_intelligent1_DK_diff_cake <- margins(model_intelligent1_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent1_DK_diff_cake)

# 2. intelligent2_DK_diff ~ treat_cake
model_intelligent2_DK_diff_cake <- lm(intelligent2_DK_diff ~ treat_cake, data = df)
summary(model_intelligent2_DK_diff_cake)
margins_intelligent2_DK_diff_cake <- margins(model_intelligent2_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent2_DK_diff_cake)

# 3. intelligent3_DK_diff ~ treat_cake
model_intelligent3_DK_diff_cake <- lm(intelligent3_DK_diff ~ treat_cake, data = df)
summary(model_intelligent3_DK_diff_cake)
margins_intelligent3_DK_diff_cake <- margins(model_intelligent3_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent3_DK_diff_cake)

# 4. intelligent4_DK_diff ~ treat_cake
model_intelligent4_DK_diff_cake <- lm(intelligent4_DK_diff ~ treat_cake, data = df)
summary(model_intelligent4_DK_diff_cake)
margins_intelligent4_DK_diff_cake <- margins(model_intelligent4_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent4_DK_diff_cake)

# 5. intelligent5_DK_diff ~ treat_cake
model_intelligent5_DK_diff_cake <- lm(intelligent5_DK_diff ~ treat_cake, data = df)
summary(model_intelligent5_DK_diff_cake)
margins_intelligent5_DK_diff_cake <- margins(model_intelligent5_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent5_DK_diff_cake)

# 6. intelligent6_DK_diff ~ treat_cake
model_intelligent6_DK_diff_cake <- lm(intelligent6_DK_diff ~ treat_cake, data = df)
summary(model_intelligent6_DK_diff_cake)
margins_intelligent6_DK_diff_cake <- margins(model_intelligent6_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent6_DK_diff_cake)

# 7. intelligent7_DK_diff ~ treat_cake
model_intelligent7_DK_diff_cake <- lm(intelligent7_DK_diff ~ treat_cake, data = df)
summary(model_intelligent7_DK_diff_cake)
margins_intelligent7_DK_diff_cake <- margins(model_intelligent7_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent7_DK_diff_cake)

# 8. intelligent8_DK_diff ~ treat_cake
model_intelligent8_DK_diff_cake <- lm(intelligent8_DK_diff ~ treat_cake, data = df)
summary(model_intelligent8_DK_diff_cake)
margins_intelligent8_DK_diff_cake <- margins(model_intelligent8_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent8_DK_diff_cake)

# 9. intelligent9_DK_diff ~ treat_cake
model_intelligent9_DK_diff_cake <- lm(intelligent9_DK_diff ~ treat_cake, data = df)
summary(model_intelligent9_DK_diff_cake)
margins_intelligent9_DK_diff_cake <- margins(model_intelligent9_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent9_DK_diff_cake)

# 10. intelligent10_DK_diff ~ treat_cake
model_intelligent10_DK_diff_cake <- lm(intelligent10_DK_diff ~ treat_cake, data = df)
summary(model_intelligent10_DK_diff_cake)
margins_intelligent10_DK_diff_cake <- margins(model_intelligent10_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent10_DK_diff_cake)

# 11. intelligent11_DK_diff ~ treat_cake
model_intelligent11_DK_diff_cake <- lm(intelligent11_DK_diff ~ treat_cake, data = df)
summary(model_intelligent11_DK_diff_cake)
margins_intelligent11_DK_diff_cake <- margins(model_intelligent11_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent11_DK_diff_cake)

# 12. intelligent12_DK_diff ~ treat_cake
model_intelligent12_DK_diff_cake <- lm(intelligent12_DK_diff ~ treat_cake, data = df)
summary(model_intelligent12_DK_diff_cake)
margins_intelligent12_DK_diff_cake <- margins(model_intelligent12_DK_diff_cake, variables = "treat_cake")
summary(margins_intelligent12_DK_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
coef_list_cake_diff <- list(
  broom::tidy(model_intelligent1_DK_diff_cake)  %>% mutate(outcome = "A"),
  broom::tidy(model_intelligent2_DK_diff_cake)  %>% mutate(outcome = "V"),
  broom::tidy(model_intelligent3_DK_diff_cake)  %>% mutate(outcome = "I"),
  broom::tidy(model_intelligent4_DK_diff_cake)  %>% mutate(outcome = "F"),
  broom::tidy(model_intelligent5_DK_diff_cake)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_intelligent6_DK_diff_cake)  %>% mutate(outcome = "M"),
  broom::tidy(model_intelligent7_DK_diff_cake)  %>% mutate(outcome = "C"),
  broom::tidy(model_intelligent8_DK_diff_cake)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_intelligent9_DK_diff_cake)  %>% mutate(outcome = "B"),
  broom::tidy(model_intelligent10_DK_diff_cake) %>% mutate(outcome = "O"),
  broom::tidy(model_intelligent11_DK_diff_cake) %>% mutate(outcome = "Å"),
  broom::tidy(model_intelligent12_DK_diff_cake) %>% mutate(outcome = "D")
)


coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---

cake_t1_dk <-  ggplot(coef_df_cake_diff, aes(x = estimate, y = outcome)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
  geom_vline(xintercept = 0, linetype = "dashed") +
    labs(
      title = "Cake: Intelligent by party, DK",
      x = "",
      y = ""
    ) +
    theme_minimal() +
    theme(
      plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
    )

  ggsave(
    filename = "Cake intelligent by party DK.jpeg",
    plot     = cake_t1_dk
  )
  
#############################################
#RELIABLE
# 1. reliable1_DK_diff ~ treat_cake
model_reliable1_DK_diff_cake <- lm(reliable1_DK_diff ~ treat_cake, data = df)
summary(model_reliable1_DK_diff_cake)
margins_reliable1_DK_diff_cake <- margins(model_reliable1_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable1_DK_diff_cake)

# 2. reliable2_DK_diff ~ treat_cake
model_reliable2_DK_diff_cake <- lm(reliable2_DK_diff ~ treat_cake, data = df)
summary(model_reliable2_DK_diff_cake)
margins_reliable2_DK_diff_cake <- margins(model_reliable2_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable2_DK_diff_cake)

# 3. reliable3_DK_diff ~ treat_cake
model_reliable3_DK_diff_cake <- lm(reliable3_DK_diff ~ treat_cake, data = df)
summary(model_reliable3_DK_diff_cake)
margins_reliable3_DK_diff_cake <- margins(model_reliable3_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable3_DK_diff_cake)

# 4. reliable4_DK_diff ~ treat_cake
model_reliable4_DK_diff_cake <- lm(reliable4_DK_diff ~ treat_cake, data = df)
summary(model_reliable4_DK_diff_cake)
margins_reliable4_DK_diff_cake <- margins(model_reliable4_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable4_DK_diff_cake)

# 5. reliable5_DK_diff ~ treat_cake
model_reliable5_DK_diff_cake <- lm(reliable5_DK_diff ~ treat_cake, data = df)
summary(model_reliable5_DK_diff_cake)
margins_reliable5_DK_diff_cake <- margins(model_reliable5_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable5_DK_diff_cake)

# 6. reliable6_DK_diff ~ treat_cake
model_reliable6_DK_diff_cake <- lm(reliable6_DK_diff ~ treat_cake, data = df)
summary(model_reliable6_DK_diff_cake)
margins_reliable6_DK_diff_cake <- margins(model_reliable6_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable6_DK_diff_cake)

# 7. reliable7_DK_diff ~ treat_cake
model_reliable7_DK_diff_cake <- lm(reliable7_DK_diff ~ treat_cake, data = df)
summary(model_reliable7_DK_diff_cake)
margins_reliable7_DK_diff_cake <- margins(model_reliable7_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable7_DK_diff_cake)

# 8. reliable8_DK_diff ~ treat_cake
model_reliable8_DK_diff_cake <- lm(reliable8_DK_diff ~ treat_cake, data = df)
summary(model_reliable8_DK_diff_cake)
margins_reliable8_DK_diff_cake <- margins(model_reliable8_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable8_DK_diff_cake)

# 9. reliable9_DK_diff ~ treat_cake
model_reliable9_DK_diff_cake <- lm(reliable9_DK_diff ~ treat_cake, data = df)
summary(model_reliable9_DK_diff_cake)
margins_reliable9_DK_diff_cake <- margins(model_reliable9_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable9_DK_diff_cake)

# 10. reliable10_DK_diff ~ treat_cake
model_reliable10_DK_diff_cake <- lm(reliable10_DK_diff ~ treat_cake, data = df)
summary(model_reliable10_DK_diff_cake)
margins_reliable10_DK_diff_cake <- margins(model_reliable10_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable10_DK_diff_cake)

# 11. reliable11_DK_diff ~ treat_cake
model_reliable11_DK_diff_cake <- lm(reliable11_DK_diff ~ treat_cake, data = df)
summary(model_reliable11_DK_diff_cake)
margins_reliable11_DK_diff_cake <- margins(model_reliable11_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable11_DK_diff_cake)

# 12. reliable12_DK_diff ~ treat_cake
model_reliable12_DK_diff_cake <- lm(reliable12_DK_diff ~ treat_cake, data = df)
summary(model_reliable12_DK_diff_cake)
margins_reliable12_DK_diff_cake <- margins(model_reliable12_DK_diff_cake, variables = "treat_cake")
summary(margins_reliable12_DK_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
coef_list_cake_diff <- list(
  broom::tidy(model_reliable1_DK_diff_cake)  %>% mutate(outcome = "A"),
  broom::tidy(model_reliable2_DK_diff_cake)  %>% mutate(outcome = "V"),
  broom::tidy(model_reliable3_DK_diff_cake)  %>% mutate(outcome = "I"),
  broom::tidy(model_reliable4_DK_diff_cake)  %>% mutate(outcome = "F"),
  broom::tidy(model_reliable5_DK_diff_cake)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_reliable6_DK_diff_cake)  %>% mutate(outcome = "M"),
  broom::tidy(model_reliable7_DK_diff_cake)  %>% mutate(outcome = "C"),
  broom::tidy(model_reliable8_DK_diff_cake)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_reliable9_DK_diff_cake)  %>% mutate(outcome = "B"),
  broom::tidy(model_reliable10_DK_diff_cake) %>% mutate(outcome = "O"),
  broom::tidy(model_reliable11_DK_diff_cake) %>% mutate(outcome = "Å"),
  broom::tidy(model_reliable12_DK_diff_cake) %>% mutate(outcome = "D")
)


coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
cake_t2_dk <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Reliable by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake reliable by party DK.jpeg",
  plot     = cake_t2_dk
)

###########################################
#SELFISH
# 1. selfish1_DK_diff ~ treat_cake
model_selfish1_DK_diff_cake <- lm(selfish1_DK_diff ~ treat_cake, data = df)
summary(model_selfish1_DK_diff_cake)
margins_selfish1_DK_diff_cake <- margins(model_selfish1_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish1_DK_diff_cake)

# 2. selfish2_DK_diff ~ treat_cake
model_selfish2_DK_diff_cake <- lm(selfish2_DK_diff ~ treat_cake, data = df)
summary(model_selfish2_DK_diff_cake)
margins_selfish2_DK_diff_cake <- margins(model_selfish2_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish2_DK_diff_cake)

# 3. selfish3_DK_diff ~ treat_cake
model_selfish3_DK_diff_cake <- lm(selfish3_DK_diff ~ treat_cake, data = df)
summary(model_selfish3_DK_diff_cake)
margins_selfish3_DK_diff_cake <- margins(model_selfish3_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish3_DK_diff_cake)

# 4. selfish4_DK_diff ~ treat_cake
model_selfish4_DK_diff_cake <- lm(selfish4_DK_diff ~ treat_cake, data = df)
summary(model_selfish4_DK_diff_cake)
margins_selfish4_DK_diff_cake <- margins(model_selfish4_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish4_DK_diff_cake)

# 5. selfish5_DK_diff ~ treat_cake
model_selfish5_DK_diff_cake <- lm(selfish5_DK_diff ~ treat_cake, data = df)
summary(model_selfish5_DK_diff_cake)
margins_selfish5_DK_diff_cake <- margins(model_selfish5_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish5_DK_diff_cake)

# 6. selfish6_DK_diff ~ treat_cake
model_selfish6_DK_diff_cake <- lm(selfish6_DK_diff ~ treat_cake, data = df)
summary(model_selfish6_DK_diff_cake)
margins_selfish6_DK_diff_cake <- margins(model_selfish6_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish6_DK_diff_cake)

# 7. selfish7_DK_diff ~ treat_cake
model_selfish7_DK_diff_cake <- lm(selfish7_DK_diff ~ treat_cake, data = df)
summary(model_selfish7_DK_diff_cake)
margins_selfish7_DK_diff_cake <- margins(model_selfish7_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish7_DK_diff_cake)

# 8. selfish8_DK_diff ~ treat_cake
model_selfish8_DK_diff_cake <- lm(selfish8_DK_diff ~ treat_cake, data = df)
summary(model_selfish8_DK_diff_cake)
margins_selfish8_DK_diff_cake <- margins(model_selfish8_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish8_DK_diff_cake)

# 9. selfish9_DK_diff ~ treat_cake
model_selfish9_DK_diff_cake <- lm(selfish9_DK_diff ~ treat_cake, data = df)
summary(model_selfish9_DK_diff_cake)
margins_selfish9_DK_diff_cake <- margins(model_selfish9_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish9_DK_diff_cake)

# 10. selfish10_DK_diff ~ treat_cake
model_selfish10_DK_diff_cake <- lm(selfish10_DK_diff ~ treat_cake, data = df)
summary(model_selfish10_DK_diff_cake)
margins_selfish10_DK_diff_cake <- margins(model_selfish10_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish10_DK_diff_cake)

# 11. selfish11_DK_diff ~ treat_cake
model_selfish11_DK_diff_cake <- lm(selfish11_DK_diff ~ treat_cake, data = df)
summary(model_selfish11_DK_diff_cake)
margins_selfish11_DK_diff_cake <- margins(model_selfish11_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish11_DK_diff_cake)

# 12. selfish12_DK_diff ~ treat_cake
model_selfish12_DK_diff_cake <- lm(selfish12_DK_diff ~ treat_cake, data = df)
summary(model_selfish12_DK_diff_cake)
margins_selfish12_DK_diff_cake <- margins(model_selfish12_DK_diff_cake, variables = "treat_cake")
summary(margins_selfish12_DK_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
coef_list_cake_diff <- list(
  broom::tidy(model_selfish1_DK_diff_cake)  %>% mutate(outcome = "A"),
  broom::tidy(model_selfish2_DK_diff_cake)  %>% mutate(outcome = "V"),
  broom::tidy(model_selfish3_DK_diff_cake)  %>% mutate(outcome = "I"),
  broom::tidy(model_selfish4_DK_diff_cake)  %>% mutate(outcome = "F"),
  broom::tidy(model_selfish5_DK_diff_cake)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_selfish6_DK_diff_cake)  %>% mutate(outcome = "M"),
  broom::tidy(model_selfish7_DK_diff_cake)  %>% mutate(outcome = "C"),
  broom::tidy(model_selfish8_DK_diff_cake)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_selfish9_DK_diff_cake)  %>% mutate(outcome = "B"),
  broom::tidy(model_selfish10_DK_diff_cake) %>% mutate(outcome = "O"),
  broom::tidy(model_selfish11_DK_diff_cake) %>% mutate(outcome = "Å"),
  broom::tidy(model_selfish12_DK_diff_cake) %>% mutate(outcome = "D")
)


coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
cake_t3_dk <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Selfish by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake selfish by party DK.jpeg",
  plot     = cake_t3_dk
)

###################################################
#MEAN
# 1. mean1_DK_diff ~ treat_cake
model_mean1_DK_diff_cake <- lm(mean1_DK_diff ~ treat_cake, data = df)
summary(model_mean1_DK_diff_cake)
margins_mean1_DK_diff_cake <- margins(model_mean1_DK_diff_cake, variables = "treat_cake")
summary(margins_mean1_DK_diff_cake)

# 2. mean2_DK_diff ~ treat_cake
model_mean2_DK_diff_cake <- lm(mean2_DK_diff ~ treat_cake, data = df)
summary(model_mean2_DK_diff_cake)
margins_mean2_DK_diff_cake <- margins(model_mean2_DK_diff_cake, variables = "treat_cake")
summary(margins_mean2_DK_diff_cake)

# 3. mean3_DK_diff ~ treat_cake
model_mean3_DK_diff_cake <- lm(mean3_DK_diff ~ treat_cake, data = df)
summary(model_mean3_DK_diff_cake)
margins_mean3_DK_diff_cake <- margins(model_mean3_DK_diff_cake, variables = "treat_cake")
summary(margins_mean3_DK_diff_cake)

# 4. mean4_DK_diff ~ treat_cake
model_mean4_DK_diff_cake <- lm(mean4_DK_diff ~ treat_cake, data = df)
summary(model_mean4_DK_diff_cake)
margins_mean4_DK_diff_cake <- margins(model_mean4_DK_diff_cake, variables = "treat_cake")
summary(margins_mean4_DK_diff_cake)

# 5. mean5_DK_diff ~ treat_cake
model_mean5_DK_diff_cake <- lm(mean5_DK_diff ~ treat_cake, data = df)
summary(model_mean5_DK_diff_cake)
margins_mean5_DK_diff_cake <- margins(model_mean5_DK_diff_cake, variables = "treat_cake")
summary(margins_mean5_DK_diff_cake)

# 6. mean6_DK_diff ~ treat_cake
model_mean6_DK_diff_cake <- lm(mean6_DK_diff ~ treat_cake, data = df)
summary(model_mean6_DK_diff_cake)
margins_mean6_DK_diff_cake <- margins(model_mean6_DK_diff_cake, variables = "treat_cake")
summary(margins_mean6_DK_diff_cake)

# 7. mean7_DK_diff ~ treat_cake
model_mean7_DK_diff_cake <- lm(mean7_DK_diff ~ treat_cake, data = df)
summary(model_mean7_DK_diff_cake)
margins_mean7_DK_diff_cake <- margins(model_mean7_DK_diff_cake, variables = "treat_cake")
summary(margins_mean7_DK_diff_cake)

# 8. mean8_DK_diff ~ treat_cake
model_mean8_DK_diff_cake <- lm(mean8_DK_diff ~ treat_cake, data = df)
summary(model_mean8_DK_diff_cake)
margins_mean8_DK_diff_cake <- margins(model_mean8_DK_diff_cake, variables = "treat_cake")
summary(margins_mean8_DK_diff_cake)

# 9. mean9_DK_diff ~ treat_cake
model_mean9_DK_diff_cake <- lm(mean9_DK_diff ~ treat_cake, data = df)
summary(model_mean9_DK_diff_cake)
margins_mean9_DK_diff_cake <- margins(model_mean9_DK_diff_cake, variables = "treat_cake")
summary(margins_mean9_DK_diff_cake)

# 10. mean10_DK_diff ~ treat_cake
model_mean10_DK_diff_cake <- lm(mean10_DK_diff ~ treat_cake, data = df)
summary(model_mean10_DK_diff_cake)
margins_mean10_DK_diff_cake <- margins(model_mean10_DK_diff_cake, variables = "treat_cake")
summary(margins_mean10_DK_diff_cake)

# 11. mean11_DK_diff ~ treat_cake
model_mean11_DK_diff_cake <- lm(mean11_DK_diff ~ treat_cake, data = df)
summary(model_mean11_DK_diff_cake)
margins_mean11_DK_diff_cake <- margins(model_mean11_DK_diff_cake, variables = "treat_cake")
summary(margins_mean11_DK_diff_cake)

# 12. mean12_DK_diff ~ treat_cake
model_mean12_DK_diff_cake <- lm(mean12_DK_diff ~ treat_cake, data = df)
summary(model_mean12_DK_diff_cake)
margins_mean12_DK_diff_cake <- margins(model_mean12_DK_diff_cake, variables = "treat_cake")
summary(margins_mean12_DK_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
coef_list_cake_diff <- list(
  broom::tidy(model_mean1_DK_diff_cake)  %>% mutate(outcome = "A"),
  broom::tidy(model_mean2_DK_diff_cake)  %>% mutate(outcome = "V"),
  broom::tidy(model_mean3_DK_diff_cake)  %>% mutate(outcome = "I"),
  broom::tidy(model_mean4_DK_diff_cake)  %>% mutate(outcome = "F"),
  broom::tidy(model_mean5_DK_diff_cake)  %>% mutate(outcome = "Æ"),
  broom::tidy(model_mean6_DK_diff_cake)  %>% mutate(outcome = "M"),
  broom::tidy(model_mean7_DK_diff_cake)  %>% mutate(outcome = "C"),
  broom::tidy(model_mean8_DK_diff_cake)  %>% mutate(outcome = "Ø"),
  broom::tidy(model_mean9_DK_diff_cake)  %>% mutate(outcome = "B"),
  broom::tidy(model_mean10_DK_diff_cake) %>% mutate(outcome = "O"),
  broom::tidy(model_mean11_DK_diff_cake) %>% mutate(outcome = "Å"),
  broom::tidy(model_mean12_DK_diff_cake) %>% mutate(outcome = "D")
)


coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c(
  "A", "V", "I", "F",
  "Æ", "M", "C", "Ø",
  "B", "O", "Å", "D"
)

coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
cake_t4_dk <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Mean by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake mean by party DK.jpeg",
  plot     = cake_t4_dk
)
